import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
import _InputBase from "@mui/material/InputBase";
import React, { useMemo } from "react";
import classes from "./sendNFT.module.less";
import utilsFilters from "../../../utils/filters";
import utilsBase from "../../../utils/basc";
import { useReactive } from "ahooks";
import useServices from "../../../hooks/useRequest";
import { getGas, tokenTransfer } from "../../../api/wallet";
import classNames from "classnames";
import toast from "../../../compoents/toast";
import { useDispatch } from "react-redux";
import { debounceFn, getMapFinalValue, getChainName, getSpec } from "../../../utils";
import { CONFLUX } from "../../../config";
import useCalculateAmount from "../../../hooks/useCalculateAmount";
import { useTranslation } from "react-i18next";
import { LocalKeyMap } from "../../../local";
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
function SendNft(props) {
    var t = useTranslation().t;
    var tipBody = useMemo(function() {
        return /*#__PURE__*/ _jsx("div", {
            className: classes.tip,
            children: /*#__PURE__*/ _jsx("div", {
                className: classes.tip_content,
                children: t(LocalKeyMap.insufficientBalance)
            })
        });
    }, []);
    var successBody = useMemo(function() {
        return /*#__PURE__*/ _jsx("div", {
            className: classes.tip,
            children: /*#__PURE__*/ _jsx("div", {
                className: "".concat(classes.tip_content, " ").concat(classes.success),
                children: t(LocalKeyMap["sendToken.success"])
            })
        });
    }, []);
    var state = useReactive({
        gasfee: "0",
        address: "",
        error: true,
        sended: false
    });
    var dispatch = useDispatch();
    var toFormatEther = useCalculateAmount().toFormatEther;
    var services = useServices({
        tokenTransfer: tokenTransfer,
        getGas: getGas
    });
    var fetchGas = function() {
        var _ref = _async_to_generator(function() {
            var _ref, code, data, e;
            return _ts_generator(this, function(_state) {
                switch(_state.label){
                    case 0:
                        if (!state.address) return [
                            2
                        ];
                        _state.label = 1;
                    case 1:
                        _state.trys.push([
                            1,
                            3,
                            ,
                            4
                        ]);
                        return [
                            4,
                            services.getGas({
                                spec: "ERC721",
                                chainType: props.chainType,
                                address: state.address
                            })
                        ];
                    case 2:
                        _ref = _state.sent(), code = _ref.code, data = _ref.data;
                        if (code == 200) {
                            state.gasfee = data;
                        }
                        return [
                            3,
                            4
                        ];
                    case 3:
                        e = _state.sent();
                        return [
                            3,
                            4
                        ];
                    case 4:
                        return [
                            2
                        ];
                }
            });
        });
        return function fetchGas() {
            return _ref.apply(this, arguments);
        };
    }();
    var send = function() {
        services.tokenTransfer({
            tokenId: props.tokenId,
            contract: props.contract,
            to: state.address,
            spec: props.spec,
            chainType: props.chainType
        }).then(function(res) {
            dispatch.app.setUserInfo();
            if (res.code == 200) {
                var _props_deleteNFT;
                (_props_deleteNFT = props.deleteNFT) === null || _props_deleteNFT === void 0 ? void 0 : _props_deleteNFT.call(props, props.id);
                state.address = "";
                state.sended = true;
                toast({
                    content: successBody,
                    centered: true,
                    duration: 1500
                });
                /** 返回列表 */ requestAnimationFrame(function() {
                    var _props_back;
                    (_props_back = props.back) === null || _props_back === void 0 ? void 0 : _props_back.call(props);
                });
            } else {
                toast.createToast({
                    type: "errToast",
                    message: res.msg
                });
            // toast({
            //   content: (
            //     <div className={classes.tip}>
            //       <div className={classes.tip_content}>{res.msg}</div>
            //     </div>
            //   ),
            //   centered: true,
            //   duration: 2500,
            // });
            }
        }).catch(function() {});
    };
    var sendClick = function() {
        if (state.error || state.sended) return;
        if (!state.address) return;
        // if (!utilsBase._checkethaddress(state.address)) {
        //   toast({
        //     content: tipBody,
        //     duration: 1500,
        //     centered: true,
        //   });
        //   return;
        // }
        var destroy = toast.confirm({
            title: /*#__PURE__*/ _jsx("div", {
                children: t(LocalKeyMap["sendNft.confirm"])
            }),
            onConfirm: function() {
                destroy();
                send();
            }
        }).destroy;
    };
    var checkAddress = debounceFn(function() {
        if (state.address) {
            if (utilsBase._checkethaddress(state.address)) {
                state.error = false;
                !CONFLUX.includes(props.chainType) && fetchGas();
            } else {
                state.error = true;
            }
        } else {
            state.error = true;
        }
    });
    var checkToast = function() {
        if (!state.address) return;
        !CONFLUX.includes(props.chainType) && fetchGas();
        if (!utilsBase._checkethaddress(state.address)) {
            toast.createToast({
                type: "errToast",
                message: "Please enter a valid address."
            });
        }
    };
    // useMount(() => {
    //   // conflux 链不请求gas
    // });
    return /*#__PURE__*/ _jsx("div", {
        className: classes.send_nft,
        children: /*#__PURE__*/ _jsxs("div", {
            className: classes["send_nft_wrap"],
            children: [
                /*#__PURE__*/ _jsx("div", {
                    className: classes.info,
                    children: /*#__PURE__*/ _jsxs("div", {
                        className: classes.head,
                        children: [
                            /*#__PURE__*/ _jsx("div", {
                                className: classes["nft_img"],
                                children: /*#__PURE__*/ _jsx("img", {
                                    // src={getNftImgSrc(props.tokenId, {
                                    //   chainType: props.chainType,
                                    //   contract: props.contract,
                                    // })}
                                    src: props.logoUrl
                                })
                            }),
                            /*#__PURE__*/ _jsxs("div", {
                                className: classes.detail,
                                children: [
                                    /*#__PURE__*/ _jsx("div", {
                                        className: classes.name,
                                        children: props.name
                                    }),
                                    /*#__PURE__*/ _jsxs("div", {
                                        className: classes.description,
                                        children: [
                                            /*#__PURE__*/ _jsxs("div", {
                                                className: classes.desc,
                                                children: [
                                                    /*#__PURE__*/ _jsx("span", {
                                                        children: t(LocalKeyMap.tokenStandard)
                                                    }),
                                                    /*#__PURE__*/ _jsx("span", {
                                                        children: getSpec(props.chainType, props.spec)
                                                    })
                                                ]
                                            }),
                                            /*#__PURE__*/ _jsxs("div", {
                                                className: classes.desc,
                                                children: [
                                                    /*#__PURE__*/ _jsx("span", {
                                                        children: t(LocalKeyMap.tokenID)
                                                    }),
                                                    /*#__PURE__*/ _jsx("span", {
                                                        children: props.tokenId
                                                    })
                                                ]
                                            }),
                                            /*#__PURE__*/ _jsxs("div", {
                                                className: classes.desc,
                                                children: [
                                                    /*#__PURE__*/ _jsx("span", {
                                                        children: t(LocalKeyMap.contractAddress)
                                                    }),
                                                    /*#__PURE__*/ _jsx("span", {
                                                        children: utilsFilters.stringMethod(props.contract, 10)
                                                    })
                                                ]
                                            })
                                        ]
                                    })
                                ]
                            })
                        ]
                    })
                }),
                /*#__PURE__*/ _jsxs("div", {
                    className: classes.send_address,
                    children: [
                        /*#__PURE__*/ _jsx("div", {
                            className: classes.title,
                            children: t(LocalKeyMap["sendToken.to"])
                        }),
                        /*#__PURE__*/ _jsx("div", {
                            className: classes.desc_tip,
                            children: t(LocalKeyMap["address.tip"])
                        }),
                        /*#__PURE__*/ _jsx("div", {
                            className: classes.address,
                            children: /*#__PURE__*/ _jsx(_InputBase, {
                                value: state.address,
                                onChange: function(e) {
                                    state.address = e.target.value;
                                    checkAddress();
                                },
                                placeholder: t(LocalKeyMap["address.placeholder"])
                            })
                        })
                    ]
                }),
                /*#__PURE__*/ _jsxs("div", {
                    className: classes.gas,
                    children: [
                        /*#__PURE__*/ _jsx("span", {
                            children: t(LocalKeyMap.transferFee)
                        }),
                        /*#__PURE__*/ _jsxs("span", {
                            className: classes.value,
                            children: [
                                toFormatEther(state.gasfee, {
                                    contract: "n/a",
                                    chainType: props.chainType
                                }),
                                "\xa0",
                                getMapFinalValue([
                                    getChainName(props.chainType)
                                ], {
                                    BSC: "BNB",
                                    ETHEREUM: "ETH",
                                    CONFLUX: "CFX"
                                })
                            ]
                        })
                    ]
                }),
                /*#__PURE__*/ _jsx("div", {
                    onClick: sendClick,
                    className: classNames(classes["sned_btn"], state.error || state.sended ? classes["btn_disabled"] : ""),
                    children: /*#__PURE__*/ _jsx("span", {
                        children: t(LocalKeyMap.send)
                    })
                })
            ]
        })
    });
}
export default SendNft;

 //# sourceMappingURL=index.js.map